package org.example.myleet.p1668;

import java.util.Arrays;

public class Solution {
    public int maxRepeating(String sequence, String word) {
        int maxRepeat = 0;
        int[] dp = new int[sequence.length()];
        Arrays.fill(dp, -1);
        for (int i = 0; i < sequence.length(); ++i) {
            if (dp[i] > -1) continue;
            int p = sequence.indexOf(word, i);
            if (p < 0) continue;
            int pc = p;
            if (dp[pc] > -1) continue;
            int k = 1;
            p += word.length();
            boolean find = true;
            while (find) {
                for (int w = 0; w < word.length(); ++w) {
                    if (p + w >= sequence.length() || sequence.charAt(p + w) != word.charAt(w)) {
                        find = false;
                        break;
                    }
                }
                if (find) {
                    ++k;
                    p += word.length();
                }
            }
            maxRepeat = Math.max(maxRepeat, k);
            dp[pc] = maxRepeat;
        }
        return maxRepeat;
    }
}
